<?php

require APPPATH . 'core/feed/feed_bank.php';

class faq extends CI_Controller {

    protected $data;

    function __construct() {
        $this->_initdata();
    }

    function index() {
        $this->data['page_title'] = "Hỏi Đáp";
        $this->data['page_css'] = "hoi-dap";
        $this->data['keyword'] = "Nhập nội dung cần tìm";
        $this->data['page_name'] = "faq";

        //get FAQ list (box Hoi/Dap)
        $page_paging = $this->config->item('base_url') . "hoi-dap/page";
        $this->show_faq_list($page_paging);

        $this->data['aFAQCate'] = $this->Fe_faq_model->_get_faq_cate_list();

        $this->show($this->data);
    }

    //Controller: search
    function search() {
        //submit search Hoi Dap.
        if (!empty($_POST['isPost']) || ($this->uri->segment(3) != "")) { //from URL
            $isSearch = 1;
            $keyWord = $_POST['txt_FAQ_Keyword'];
            $faqCate = $_POST['hidCateId'];
            $cateName = $_POST['txt_FAQ_Cate'];
        }

        if ($isSearch) {
            $iCurPage = 1;
            $iCurPage = $_POST['h_cur_page'];
            $iRow_Count = 0;
            @$limit = PAGESIZE;
            $start = $iCurPage * PAGESIZE - PAGESIZE;
            if (!($start > 0)) {
                $start = 0;
            }
            $eu = ($start - 0);
            if (!$limit > 0) {
                $limit = 20;
            }
            list($iRow_Count, $this->data['aFAQ']) = $this->Fe_faq_model->_search_faq_list($keyWord, $faqCate, $eu, $limit);

            //create criteria for paging search:
            $this->data['paging'] = $this->_paging_search($iCurPage, ceil($iRow_Count / PAGESIZE), $this->config->item('base_url') . "hoi-dap/tim-kiem", $iRow_Count);
            $this->data['cur_page'] = $iCurPage;
						$this->data['pagesize'] = PAGESIZE;
            $this->data['txt_Faq_Keyword'] = $keyWord;
            $this->data['cate_id'] = $faqCate;
            $this->data['cate_name'] = $cateName;
            $this->data['city_id'] = $city;
            $this->data['aFAQCate'] = $this->Fe_faq_model->_get_faq_cate_list();

            //data for layout.
            $this->data['page_title'] = "Hỏi Đáp";
            $this->data['page_css'] = "hoi-dap";
            $this->data['keyword'] = "Nhập nội dung cần tìm";
            $this->data['page_name'] = "faq";
            $this->data['search'] = 1;
            $this->data['previous_id'] = 1;
            $this->data['msg'] = "";
            $this->show($this->data);
        }
    }

    function show_faq_list($page_paging) {
        $iCurPage = 1;
        $iCurPage = $this->uri->segment(3);
				if ($iCurPage == "") $iCurPage=1;
        $iRow_Count = 0;
        @$limit = PAGESIZE;
        $start = $iCurPage * PAGESIZE - PAGESIZE;
        if (!($start > 0)) {
            $start = 0;
        }
        $eu = ($start - 0);
        if (!$limit > 0) {
            $limit = 20;
        }
        list($iRow_Count, $this->data['aFAQ']) = $this->Frontendmodel->_get_faq_paging($eu, $limit);
        $this->data['paging'] = $this->paging($iCurPage, ceil($iRow_Count / PAGESIZE), $page_paging, $iRow_Count);
        $this->data['cur_page'] = $iCurPage;
				$this->data['pagesize'] = PAGESIZE;
        $this->data['previous_id'] = 1;
    }

    //Phân trang news
    function paging($page, $totalpage, $pagelink, $totalrows) {
        if ($page == "")
            $page = 1;

        $this->data['cur_page'] = $page;

        $paging_str = '';
        if ($page > 1)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="location.href=' . "'" . $pagelink . '/' . ($page - 1) . "'" . '"><img src="' . $this->config->item('base_url') . 'style/images/img20.gif" border="0"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('base_url') . 'style/images/img20.gif" border="0"></a>';

        // list 10 pagelink
        $startpage = 1;
        if ($page > 5)
            $startpage = $page - 4;
        $endpage = $totalpage;
        if ($totalpage - $page > 5)
            $endpage = $page + 5;
        for ($i = $startpage; $i < $page; $i++)
            $paging_str .= '<a href="' . $pagelink . '/' . $i . '" class="paging-item">' . $i . '</a>';

        $paging_str .= '<a href="' . $pagelink . '/' . $i . '" class="paging-item-selected">' . $page . '</a>';

        for ($i = $page + 1; $i <= $endpage; $i++)
            $paging_str .= '<a href="' . $pagelink . '/' . $i . '" class="paging-item">' . $i . '</a>';

        if ($page < $totalpage)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="location.href=' . "'" . $pagelink . '/' . ($page + 1) . "'" . '"><img src="' . $this->config->item('base_url') . 'style/images/img21.gif" border="0"></a>';
        //$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' ."'".$pagelink.'/'.($page+1).".html'". '">';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('base_url') . 'style/images/img21.gif" border="0"></a>';
        $paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';

        //$paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' .$totalrows. '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
        return $paging_str;
    }

    //local func: phân trang dùng cho function "Tìm Kiếm"
    function _paging_search($page, $totalpage, $pagelink, $totalrows) {
				//echo "\$page=".$page."<br>";
				//echo "\$totalpage=".$totalpage."<br>";
				//echo "\$pagelink=".$pagelink."<br>";
				//echo "\$totalrows=".$totalrows."<br>";
        $paging_str = '';
        if ($page > 1)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="submitSearch(\'' . $pagelink . '\',' . ($page - 1) . ');return true;"><img src="' . $this->config->item('base_url') . 'style/images/img20.gif" style="cursor:pointer;"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('base_url') . 'style/images/img20.gif" align="absmiddle"></a>';
        $paging_str .= '&nbsp;&nbsp;&nbsp;';

        // list 10 pagelink
        $startpage = 1;
        if ($page > 5)
            $startpage = $page - 4;
        $endpage = $totalpage;
        if ($totalpage - $page > 5)
            $endpage = $page + 5;
        for ($i = $startpage; $i < $page; $i++)
            $paging_str .= ' <a onclick="submitSearch(\'' . $pagelink . '\',' . $i . ');return true;" class="paging-item" href="javascript:void(0);">' . $i . '</a>';
        //$paging_str .= ' <a onclick="submitSearch(\'"'.$pagelink. '></a>';;
        //$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link_selected">' .$page. '</a>';
        $paging_str .= ' <a onclick="submitSearch(\'' . $pagelink . '\',' . $i . ');return true;" class="paging-item-selected" href="javascript:void(0);">' . $i . '</a>';
        for ($i = $page + 1; $i <= $endpage; $i++)
            $paging_str .= ' <a onclick="submitSearch(\'' . $pagelink . '\',' . $i . ');return true;" class="paging-item" href="javascript:void(0);">' . $i . '</a>';
        //$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';

        $paging_str .= '&nbsp;&nbsp;&nbsp;';
        if ($page < $totalpage)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="submitSearch(\'' . $pagelink . '\',' . ($page + 1) . ');return true;"><img src="' . $this->config->item('base_url') . 'style/images/img21.gif" style="cursor:pointer;"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('base_url') . 'style/images/img21.gif" align="absmiddle"></a>';
        $paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';
        return $paging_str;
    }

    function _check_page_views($id) {
        if ($this->Frontendmodel->_check_views("PAGEVIEWS") == false) {
            $page_views = $this->Frontendmodel->_update_config($id);
            $this->Frontendmodel->_set_session("PAGEVIEWS", $page_views);
        }
    }

    function _initdata() {
        parent::__construct();
        session_start();
        $this->load->helper('eximcached');
        
        $this->load->model('Frontendmodel');
        $this->load->model('Fe_adv_model');
        $this->load->model('Fe_company_model');
        $this->load->model('Fe_docs_model');
        $this->load->model('Fe_forwarding_model');
        $this->load->model('Fe_knowledge_model');
        $this->load->model('Fe_faq_model');
        
        //load submenu and footer
        list($this->data['aLatestNews']) = get_latest_news($this->Frontendmodel, 7);
        list($this->data['aCate2']) = get_newscate_list(NEWS_CATE_VBPQ, $this->Frontendmodel);
        $this->data['aCate3'] = $this->Fe_knowledge_model->get_all_cate();
        $this->data['aCate4'] = _get_all_cate_customs($this->Frontendmodel);
        $this->data['aCate5'] = $this->Fe_forwarding_model->get_all_cate();
        $this->data['aCate6'] = $this->Fe_faq_model->_get_faq_cate_list();
        
        $this->data['page_header'] = "header";
        $this->data['page_rightbar'] = "rightbar";
        $this->data['page_footer'] = "footer";
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['img_url'] = $this->config->item('img_url');
        $this->data['current_url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $this->data['aCityList'] = get_cities_list($this->Frontendmodel);
        $feed = new feed_bank();
        $this->data['gold'] = get_feed_gold($feed);
        $this->data['exchange_rate'] = get_feed_exchange($feed);

    }

}

?>